<template>
  <Row :gutter="16">
    <Col :span="12">
      <Card title="累计盘点件数" :loading="loading">
        <InventoryCount />
      </Card>
    </Col>
    <Col :span="12">
      <Card title="近期盘点异常" :loading="loading">
        <AbnormalInventory />
      </Card>
    </Col>

    <Col style="margin-top: 16px" :span="12">
      <Card title="资产分类异常排行" :loading="loading">
        <AssetCategoryRanking />
      </Card>
    </Col>
    <Col style="margin-top: 16px" :span="12">
      <Card title="部门异常排行" :loading="loading">
        <DepartmentRanking />
      </Card>
    </Col>
  </Row>
</template>
<script lang="ts" setup>
  import { ref, onMounted, computed, Ref } from 'vue';
  import { Card, Row, Col } from 'ant-design-vue';
  import AbnormalInventory from './components/AbnormalInventory.vue';
  import AssetCategoryRanking from './components/AssetCategoryRanking.vue';
  import DepartmentRanking from './components/DepartmentRanking.vue';
  import InventoryCount from './components/InventoryCount.vue';
  import { list } from '@/views/asset/inventory-manage/InventoryManage.api';
  const loading = ref(true);
  // const inventoryList = ref([]);
  interface InventoryItem {
    count: number;
    acturalCount: number;
    position: string;
    acturalPosition: string;
  }

  const inventoryList: Ref<Array<InventoryItem>> = ref([]);
  const normalList = ref([]);
  const lossList = ref([]);
  const surplusList = ref([]);
  const positionDiffList = ref([]);
  const abnormalList = ref([]);
  onMounted(async () => {
    // 异步操作
    // await new Promise((resolve) => setTimeout(resolve, 100));
    const result = await list({});
    inventoryList.value = result.records;
    loading.value = false;
    // console.log('###', inventoryList.value);

    // 按照盘点异常类型进行分类
    inventoryList.value.forEach((item) => {
      // 假设 count 是正常数量，acturalCount 是实际数量
      if (item.count === item.acturalCount) {
        normalList.value.push(item);
      } else if (item.count > item.acturalCount) {
        lossList.value.push(item);
      } else if (item.count < item.acturalCount) {
        surplusList.value.push(item);
      }
      // 假设 position 是原来位置，acturalPosition 是实际位置
      if (item.position !== item.acturalPosition) {
        positionDiffList.value.push(item);
      }
    });
  });
  // inventoryList.value.forEach((item) => {
  //   // 假设 count 是正常数量，acturalCount 是实际数量
  //   if (item.count !== item.acturalCount || item.position !== item.acturalPosition) {
  //     abnormalList.value.push(item);
  //   }
  // });
  inventoryList.value.forEach((item) => {
    // 假设 count 是正常数量，acturalCount 是实际数量
    if (item.inventoryState === '1') {
      abnormalList.value.push(item);
    }
  });
  console.log(abnormalList, '###');

  // 对分类后的数据进行排行
  const rankedData = computed(() => {
    // 假设按照月份进行排行
    const monthRank = {};
    inventoryList.value.forEach((item) => {
      // 假设 date 是盘点单的日期字段，格式为 YYYY-MM-DD
      const month = item.createTime.slice(0, 7); // 提取月份部分
      if (!monthRank[month]) {
        monthRank[month] = [];
      }
      monthRank[month].push(item);
    });

    // 假设按照资产分类进行排行
    const assetRank = {};
    inventoryList.value.forEach((item) => {
      // 假设 asset 是资产分类字段
      const assetCategory = item.assetCategory;
      if (assetCategory) {
        if (!assetRank[assetCategory]) {
          assetRank[assetCategory] = [];
        }
        assetRank[assetCategory].push(item);
      }
    });

    // 假设按照部门进行排行
    const departmentRank = {};
    inventoryList.value.forEach((item) => {
      // 假设 department 是部门字段
      const department = item.department;
      if (department) {
        if (!departmentRank[department]) {
          departmentRank[department] = [];
        }
        departmentRank[department].push(item);
      }
    });
    return {
      monthRank,
      assetRank,
      departmentRank,
    };
  });
</script>
<style scoped>
  .ant-row {
    padding: 16px;
  }
  .ant-card-loading {
    :deep(.ant-card-body) {
      height: 333px !important;
    }
  }
</style>
